package com.demo.accessibilitykeeper;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * 恢复出厂设置保护设备管理员
 */
public class FactoryResetProtectionReceiver extends DeviceAdminReceiver {
    private static final String TAG = "FactoryResetProtection";

    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Log.i(TAG, "🛡️ FACTORY_RESET_PROTECTION_ENABLED - Device admin activated");
        recordDeviceAdminActivation(context);
        showProtectionNotification(context, "恢复出厂设置保护已启用");
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Log.w(TAG, "⚠️ FACTORY_RESET_PROTECTION_DISABLED - Device admin deactivated");
        recordDeviceAdminDeactivation(context);
        showProtectionNotification(context, "恢复出厂设置保护已禁用");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        Log.w(TAG, "🚨 FACTORY_RESET_PROTECTION_DISABLE_REQUESTED - User trying to disable");
        recordDeviceAdminDisableRequest(context);
        return "⚠️ 禁用设备管理员将降低应用保护能力！\n\n" +
               "恢复出厂设置时应用可能被卸载。\n\n" +
               "确定要禁用吗？";
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.i(TAG, "🔐 PASSWORD_CHANGED - Security enhanced");
        recordPasswordChange(context);
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
        Log.w(TAG, "🔐 PASSWORD_FAILED - Security breach attempt");
        recordPasswordFailure(context);
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
        Log.i(TAG, "🔐 PASSWORD_SUCCEEDED - Security access granted");
        recordPasswordSuccess(context);
    }

    /**
     * 记录设备管理员激活
     */
    private void recordDeviceAdminActivation(Context context) {
        try {
            long currentTime = System.currentTimeMillis();
            context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("device_admin_enabled", true)
                .putLong("device_admin_activation_time", currentTime)
                .putInt("device_admin_activation_count", 
                    context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                        .getInt("device_admin_activation_count", 0) + 1)
                .apply();
            
            Log.i(TAG, "📝 DEVICE_ADMIN_ACTIVATION_RECORDED");
        } catch (Exception e) {
            Log.e(TAG, "Error recording device admin activation", e);
        }
    }

    /**
     * 记录设备管理员停用
     */
    private void recordDeviceAdminDeactivation(Context context) {
        try {
            long currentTime = System.currentTimeMillis();
            context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("device_admin_enabled", false)
                .putLong("device_admin_deactivation_time", currentTime)
                .putInt("device_admin_deactivation_count", 
                    context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                        .getInt("device_admin_deactivation_count", 0) + 1)
                .apply();
            
            Log.i(TAG, "📝 DEVICE_ADMIN_DEACTIVATION_RECORDED");
        } catch (Exception e) {
            Log.e(TAG, "Error recording device admin deactivation", e);
        }
    }

    /**
     * 记录设备管理员禁用请求
     */
    private void recordDeviceAdminDisableRequest(Context context) {
        try {
            long currentTime = System.currentTimeMillis();
            context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                .edit()
                .putLong("device_admin_disable_request_time", currentTime)
                .putInt("device_admin_disable_request_count", 
                    context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                        .getInt("device_admin_disable_request_count", 0) + 1)
                .apply();
            
            Log.i(TAG, "📝 DEVICE_ADMIN_DISABLE_REQUEST_RECORDED");
        } catch (Exception e) {
            Log.e(TAG, "Error recording device admin disable request", e);
        }
    }

    /**
     * 记录密码更改
     */
    private void recordPasswordChange(Context context) {
        try {
            long currentTime = System.currentTimeMillis();
            context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                .edit()
                .putLong("password_change_time", currentTime)
                .putInt("password_change_count", 
                    context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                        .getInt("password_change_count", 0) + 1)
                .apply();
            
            Log.i(TAG, "📝 PASSWORD_CHANGE_RECORDED");
        } catch (Exception e) {
            Log.e(TAG, "Error recording password change", e);
        }
    }

    /**
     * 记录密码失败
     */
    private void recordPasswordFailure(Context context) {
        try {
            long currentTime = System.currentTimeMillis();
            context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                .edit()
                .putLong("password_failure_time", currentTime)
                .putInt("password_failure_count", 
                    context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                        .getInt("password_failure_count", 0) + 1)
                .apply();
            
            Log.i(TAG, "📝 PASSWORD_FAILURE_RECORDED");
        } catch (Exception e) {
            Log.e(TAG, "Error recording password failure", e);
        }
    }

    /**
     * 记录密码成功
     */
    private void recordPasswordSuccess(Context context) {
        try {
            long currentTime = System.currentTimeMillis();
            context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                .edit()
                .putLong("password_success_time", currentTime)
                .putInt("password_success_count", 
                    context.getSharedPreferences("factory_reset_protection_prefs", Context.MODE_PRIVATE)
                        .getInt("password_success_count", 0) + 1)
                .apply();
            
            Log.i(TAG, "📝 PASSWORD_SUCCESS_RECORDED");
        } catch (Exception e) {
            Log.e(TAG, "Error recording password success", e);
        }
    }

    /**
     * 显示保护通知
     */
    private void showProtectionNotification(Context context, String message) {
        try {
            // 这里可以添加通知显示逻辑
            Log.i(TAG, "🔔 PROTECTION_NOTIFICATION - " + message);
        } catch (Exception e) {
            Log.e(TAG, "Error showing protection notification", e);
        }
    }
}
